package com.sl.au.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.sl.au.entity.EventReport;
import com.sl.au.entity.User;
import com.sl.au.service.EventReportService;
import com.sl.au.service.OptionService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping("/ui/visitorTask")
public class VisitorTaskController extends BaseController {

	@Autowired
	private EventReportService eventReportService;
	@Autowired
	private OptionService optionService;

	@RequestMapping("/initInit")
	public MsgHelper initInit() {
		// String device = request.getParameter("device");
		// String identify = request.getParameter("identify");
		String user = this.getMyAccount().getUser().getId();
		String type = optionService.getRepository().findByGroupAndTitle("事件上报状态", "初始化").get(0).getId();
		List<EventReport> inits = eventReportService.getReportPageByUserAndType(0, 30, user, type, "createTime",
				Sort.Direction.DESC);
		return MsgHelper.success().put("inits", inits);
	}

	@RequestMapping("/initOvers")
	public MsgHelper initOvers() {
		// String device = request.getParameter("device");
		// String identify = request.getParameter("identify");
		String user = this.getMyAccount().getUser().getId();
		String type1 = optionService.getRepository().findByGroupAndTitle("事件上报状态", "已归档").get(0).getId();
		String type2 = optionService.getRepository().findByGroupAndTitle("事件上报状态", "已完结").get(0).getId();
		String[] types = new String[] { type1, type2 };
		List<EventReport> overs = eventReportService.getReportPageByUserAndTypes(0, 30, user, types, "createTime",
				Sort.Direction.DESC);
		return MsgHelper.success().put("overs", overs);
	}

	@RequestMapping("/initHasDid")
	public MsgHelper initHasDid() {
		// String device = request.getParameter("device");
		// String identify = request.getParameter("identify");
		String user = this.getMyAccount().getUser().getId();
		String type = optionService.getRepository().findByGroupAndTitle("事件上报状态", "已受理").get(0).getId();
		List<EventReport> hasDid = eventReportService.getReportPageByUserAndType(0, 30, user, type, "createTime",
				Sort.Direction.DESC);
		return MsgHelper.success().put("hasDid", hasDid);
	}

	@RequestMapping("/findReportByReportId/{reportId}")
	public MsgHelper findReportByReportId(@PathVariable("reportId") String reportId) {
		EventReport report = eventReportService.getRepository().findOne(reportId);
		return MsgHelper.success().put("report", report);
	}

	@RequestMapping("/initHistory")
	public MsgHelper initHistory() {
		// String device = request.getParameter("device");
		// String identify = request.getParameter("identify");
		User user = this.getMyAccount().getUser();
		List<EventReport> histories = eventReportService.getEventReportByUser(0, 20, user, "createTime",
				Sort.Direction.DESC);
		return MsgHelper.success().put("histories", histories);
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		String device = request.getParameter("device");
		String type = request.getParameter("type");
		// String identify = request.getParameter("identify");
		if (this.DEVICE.equals(device)) {
			modelAndView.setViewName("mobile/visitorTask/index");
			modelAndView.addObject("user", getMyAccount().getUser());
			modelAndView.addObject("type", type);
		} else {
			// pc端
			// modelAndView = createLayoutView("archive/index");
		}
		return modelAndView;
	}
}
